<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="author" content="lcy">
    <script src="jquery-3.1.1.js"></script>
    <title></title>
    <style>
        .content{
            width: 100%;
            height: 2000px;
            background-color: skyblue;
        }
        .overlay{
            height: 100%;
            width: 100%;
            background-color: rgba(0, 0, 0, 0.2);
            position: absolute;
            top: 0;
            left: 0;
            z-index: 999;
            display: none;
        }
        .alert{
            width: 400px;
            height: 200px;
            background-color: #fff;
            position: absolute;
            top: 100px;
            left: 100px;
            z-index: 1000;
            display: none;
        }
        .alert h2{
            padding: 0;
            margin: 0;
            height: 40px;
            line-height: 40px;
            text-align: center;
            background-color: #ccc;
            position: relative;
            cursor: move;
            /*禁止用户选择页面上的内容*/
            user-select: none;
            -webkit-user-select: none;
            -moz-user-select: none;
        }
        .close{
            position: absolute;
            right: 5px;
            width: 30px;
            height: 30px;
            transition: all 2s linear;
            line-height: 30px;
            top: 5px;
        }
        .close:hover{
            background-color: #eee;
        }
        form{
            width: 100%;
            height: 160px;
            padding: 20px;
        }
        form div{
            line-height: 40px;
            text-align: center;
        }
        form input{
            width: 260px;
            height: 30px;
        }
    </style>
</head>
<body>
    <div class="content"></div>
    <center><button type="button">dianwo</button></center>
    <div class="overlay"></div>
    <div class="alert">
        <h2><span>用户登录</span><span class="close">&times;</span></h2>
        <form action="">
            <div><label>用户名：<input type="text" placeholder="请输入用户名"></label></div>
            <div><label>密码：<input type="password" placeholder="请输入密码"></label></div>
            <div><input type="submit" value="提交"></div>
        </form>
    </div>
</body>
</html>
<script>
    $(function(){
        // 先用一个方法设置遮罩层的大小
        overlay()
        function overlay(){
            var w=$(document).width();
            var h=$(document).height();
            $('.overlay').width(w).height(h);
        }
        alert()
        function alert(){
            var left=($(window).width()-$('.alert').width())/2+$(document).scrollLeft();
            var top=($(window).height()-$('.alert').height())/2+$(document).scrollTop();

            // console.log(left+'ddddddd'+top);/
            $('.alert').css({
                left:left,
                top:top,
            })
        }
        $(window).resize(function(){
            alert();
            overlay();
        });
        $(window).scroll(function(){
            alert();
            overlay();
        })
        $('button').click(function(){
            $('.overlay').show();
            $('.alert').show();
            $('html').css('overflow','hidden');
            $('body').css('overflow','hidden');
        })
        $('.close').click(function(){
            $('.overlay').hide();
            $('.alert').hide();
            $('html').css('overflow','auto');
            $('body').css('overflow','auto');
        })
        $('.alert h2').mousedown(function(ev){
            var disx=ev.offsetX;
            var disy=ev.offsetY;
            $(document).mousemove(function(ev){
                // 获取鼠标点击位置相对于文档的位置
                var left=ev.pageX-disx;
                var top=ev.pageY-disy;
                if(left<0){
                    left=0;
                }
                if(left<=$(document).scrollLeft){
                    left=$(document).scrollLeft;
                }
                if(left>=$(window).width()-$('.alert').width()+$(document).scrollLeft()){
                    left=$(window).width()-$('.alert').width()+$(document).scrollLeft();
                }
                if(top<0){
                    top=0;
                }
                if(top<=$(document).scrollTop()){
                    top=$(document).scrollTop();
                }
                if(top>=$(window).height()-$('.alert').height()+$(document).scrollTop()){
                    top=$(window).height()-$('.alert').height()+$(document).scrollTop();
                }
                $('.alert').css({
                    left:left,
                    top:top,
                })
            })
        })
        // 松开鼠标 取消拖动
        $(document).mouseout(function(){
                $(this).off('mousemove')
            })
    });
</script>